package com.example.zhongcao.controller;

import com.alibaba.fastjson.JSON;
import com.example.zhongcao.entity.YuLanZhongcao;
import com.example.zhongcao.entity.Zhongcao;
import com.example.zhongcao.exception.InvalidParam;
import com.example.zhongcao.exception.User.InvalidOpenid;
import com.example.zhongcao.service.ZhongcaoService;
import com.example.zhongcao.utils.Message;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("zhongcao")
public class ZhongcaoController {
    @Resource
    ZhongcaoService zhongcaoService;

    @RequestMapping(value = "buildZc",method = RequestMethod.POST)
    public Message buildZhongcao( @RequestParam("openid")String openid, @RequestParam(value = "title")String title,@RequestParam(value = "intro")String intro,
                                  @RequestParam(value = "address",required = false)String address,@RequestParam(value = "author",required = false)String author,
                                  @RequestParam(value = "price",required = false)String price, @RequestParam(value = "pics",required = false)List<MultipartFile> pics,
                                  @RequestParam(value = "signs",required = false)String signStr){
        String msg="新建种草成功，可去[我的种草]中查看哦！";
        try {
            List<String> signs;
            signs = JSON.parseArray(signStr).toJavaList(String.class);
            zhongcaoService.buildZhongcao(openid,title,intro,address,author,price,pics,signs);
            return Message.createSuc(msg);
        } catch (InvalidOpenid invalidOpenid) {
            System.out.println(invalidOpenid.getErrmsg());
            return Message.createErr(invalidOpenid.getErrmsg());
        } catch (InvalidParam invalidParam) {
            System.out.println(invalidParam.getErrmsg());
            return Message.createErr(invalidParam.getErrmsg());
        } catch (Exception e) {
            e.printStackTrace();
            return Message.createErr("您的网络走丢啦，请稍后再试！");
        }

    }
    @RequestMapping(value="getMyZCs",method = RequestMethod.GET)
    public Message getMyZhongcaos(@RequestParam("openid")String openid,@RequestParam("type")String type,@RequestParam(value = "keyword",required = false)String keyword){
        try {
            List<YuLanZhongcao> zcs=zhongcaoService.getMyZCs(openid, type, keyword);
            return Message.createSuc(zcs);
        } catch (InvalidOpenid e) {
            System.out.println(e.getErrmsg());
            return Message.createErr(e.getErrmsg());
        } catch (InvalidParam e) {
            System.out.println(e.getErrmsg());
            return Message.createErr(e.getErrmsg());
        }
    }

    @RequestMapping(value="getZhongcaoDetails",method = RequestMethod.GET)
    public Message getZhongcaoDetails(@RequestParam("zhongcaoid")Integer zhongcaoid){
        try {
            return Message.createSuc(zhongcaoService.getZhongcaoDeatils(zhongcaoid));
        } catch (InvalidParam e) {
            System.out.println(e.getErrmsg());
            return Message.createErr(e.getErrmsg());
        }
    }

    @RequestMapping(value="editZhongcao",method = RequestMethod.POST)
    public Message editZhongcao(@RequestParam("zhongcaoid")Integer zhongcaoid,@RequestParam(value = "title",required = false)String title,@RequestParam(value = "intro",required = false)String intro,
                                @RequestParam(value = "author",required = false)String author,@RequestParam(value = "address",required = false)String address,
                                @RequestParam(value = "price",required = false)String price,@RequestParam(value = "pics",required = false)List<MultipartFile> pics,
                                @RequestParam(value = "signs",required = false)List<String>signs){
        String msg = "";
        Zhongcao zc=new Zhongcao(zhongcaoid,title,author,intro,address,price);
        try {
            msg="编辑成功，可去[我的种草]页面查看哦！";
            zhongcaoService.editZhongcao(zc,pics,signs);
            return Message.createSuc(msg);
        } catch (IllegalAccessException e) {
            msg="您的网络走丢了，请稍后再试！";
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            msg="您的网络走丢了，请稍后再试！";
        }
        return Message.createErr(msg);

    }
    @RequestMapping(value = "getComZCs",method = RequestMethod.GET)
    public Message getComZCs(@RequestParam("type")String type,@RequestParam(value = "keyword",required = false)String keyword,@RequestParam(value="sign",required = false)String signs){
        String msg=null;
        try {
            return Message.createSuc(zhongcaoService.getComZCs(type, keyword, signs));
        } catch (InvalidParam e) {
            System.out.println(e.getErrmsg());
            msg=e.getErrmsg();
        }
        return Message.createErr(msg);
    }

    @RequestMapping(value = "goodZhongcao",method = RequestMethod.POST)
    public Message goodZhongcao(@RequestParam("zhongcaoid")Integer zhongcaoid,@RequestParam("openid")String openid){
        String msg=null;
        try {
            msg="点赞成功哦，再继续逛逛吧！";
            zhongcaoService.goodZhongcao(zhongcaoid,openid);
            return Message.createSuc(msg);
        } catch (InvalidOpenid e) {
            System.out.println(e.getErrmsg());
            msg=e.getErrmsg();
        } catch (InvalidParam e) {
            System.out.println(e.getErrmsg());
            msg=e.getErrmsg();
        }
        return Message.createErr(msg);
    }
}
